package com.msc.cili.web.admincontroller.type;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.msc.cili.common.api.Result;
import com.msc.cili.common.constants.RedisKeyConstant;
import com.msc.cili.infrastructure.redis.RedisService;
import com.msc.cili.repository.entity.type.Type;
import com.msc.cili.service.type.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类前端控制器
 *
 * @author coder-msc
 * @date 2021/4/8
 */
@RestController
@Slf4j
public class TypeAdminController {
    @Autowired
    TypeService typeService;
    @Autowired
    RedisService redisService;

    /**
     * 查询所有分类
     */
//    @GetMapping("/types")
//    public Result blogs(@RequestParam(defaultValue = "1", value = "typeForm") Integer typeForm) {
//        log.info("进入了types方法查询");
//        if (redisService.hasHashKey(RedisKeyConstant.CATEGORY_NAME_CACHE + typeForm, RedisKeyConstant.All)) {
//            return Result.succ(redisService.getValueByHashKey(RedisKeyConstant.CATEGORY_NAME_CACHE + typeForm, RedisKeyConstant.All));
//        }
//        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(Type::getTypeForm, typeForm);
//        queryWrapper.orderByDesc("order_sort");
//        List<Type> list = typeService.list(queryWrapper);
//        redisService.saveKVToHash(RedisKeyConstant.CATEGORY_NAME_CACHE + typeForm, RedisKeyConstant.All, list);
//        return Result.succ(list);
//    }


    /**
     * 分页查询分类
     */
    @RequiresAuthentication
    @RequiresPermissions("user:read")
    @GetMapping("/type/list")
    public Result typeList(@RequestParam(value = "typeForm", defaultValue = "1") Integer typeForm, @RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize) {
        Page page = new Page(currentPage, pageSize);
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Type::getTypeForm, typeForm);
        queryWrapper.orderByDesc("order_sort");
        IPage pageData = typeService.page(page, queryWrapper);
        return Result.succ(pageData);
    }


    /**
     * 增加分类
     */
    @RequiresPermissions("user:create")
    @RequiresAuthentication
    @PostMapping("/type/create")
    public Result createType(@Validated @RequestBody Type type) {
        if (type == null) {
            return Result.fail("不能为空");
        } else {
            typeService.saveOrUpdate(type);
            redisService.deleteCacheByKey(RedisKeyConstant.CATEGORY_NAME_CACHE + type.getTypeForm());
        }
        return Result.succ(null);
    }


    /**
     * 修改分类
     */
    @RequiresPermissions("user:update")
    @RequiresAuthentication
    @PostMapping("/type/update")
    public Result updateType(@Validated @RequestBody Type type) {
        if (type == null) {
            return Result.fail("不能为空");
        } else {
            typeService.saveOrUpdate(type);
            redisService.deleteCacheByKey(RedisKeyConstant.CATEGORY_NAME_CACHE + type.getTypeForm());
        }
        return Result.succ(null);
    }


    /**
     * 删除分类
     */
    @RequiresRoles("role_root")
    @RequiresPermissions("user:delete")
    @RequiresAuthentication
    @GetMapping("/type/delete/{id}")
    public Result delete(@PathVariable(name = "id") Long id) {

        if (typeService.removeById(id)) {
            redisService.deleteCacheByKey(RedisKeyConstant.CATEGORY_NAME_CACHE+1); //博客分类清除
            redisService.deleteCacheByKey(RedisKeyConstant.CATEGORY_NAME_CACHE+2); //资源分类清除
            return Result.succ(null);
        } else {
            return Result.fail("删除失败");
        }

    }

}
